package com.ajz.security.service.impl;

import com.ajz.common.exception.BusinessException;
import com.ajz.common.service.impl.BaseServiceImpl;
import com.ajz.common.utils.Collections3;
import com.ajz.common.utils.UserUtils;
import com.ajz.security.dao.UserDao;
import com.ajz.security.entity.Role;
import com.ajz.security.entity.User;
import com.ajz.security.model.UserImportData;
import com.ajz.security.service.IUserService;
import com.ajz.security.utils.SecurityUtils;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Transactional
@Service
public class UserServiceImpl extends BaseServiceImpl<User, String> implements IUserService {
	
	@Override
	public User findByOutSn(String outSn) {
		
		return userDao.findByOutSn(outSn);
	}


	@Autowired
	private UserDao userDao;
	
	
	@Autowired
	private BCryptPasswordEncoder bpe;

	
	@Override
	public User save(User entity) {
		if (null==entity.getId()) {
			User user=this.findByLoginName(entity.getLoginName());
			if (user!=null) {
				throw new BusinessException("登录名已存在！");
			}
		}
	    if(Strings.isNullOrEmpty(entity.getPassword())){
	    	entity.setPassword(SecurityUtils.getDefaultUserPassword());
	    }else{
	    	entity.setPassword(bpe.encode(entity.getPassword()));
	    }
		
		List<String> dataPer= Lists.newArrayList(entity.getLoginName());
		entity.setDataPermission(dataPer);
		return super.save(entity);
	}

	@Override
	public User update(User entity) {
		List<String> dataPer= Lists.newArrayList(entity.getLoginName());
		entity.setDataPermission(dataPer);
		if(entity.getLoginName().equals("admin")){
			List<Role> roles=UserUtils.getUser().getRoles();
			entity.setRoles(roles);
		}

		return super.update(entity);
	}
	

	@Override
	public User findByLoginName(String loginName) {
		return userDao.findByLoginName(loginName);
	}


	@Override
	public void importUser(List<UserImportData> userImportDatas) {
		if (Collections3.isEmpty(userImportDatas)) {
			return;
		}
		for(UserImportData userImportData : userImportDatas) {
			importUser(userImportData);
		}
		
	}
	

	private void importUser(UserImportData userImportData) {
//		User user = userDao.findByLoginName(userImportData.getJobNo());
//		if (user == null) {
//			user = new User();
//			user.setLoginName(userImportData.getJobNo());
//			user.setPassword(SecurityUtils.getDefaultUserPassword());
//			user.setCreatedDate(DateUtils.now());
////			user.setCreatedBy(UserUtils.getUserName());
//		} else {
//			user.setLastModifiedDate(DateUtils.now());
////			user.setLastModifiedBy(UserUtils.getUserName());
//		}
//		user.setName(userImportData.getName());
//		user.setPostName(userImportData.getPostName());
//		
//		String[] groupCodes = userImportData.getGroupCodes().split(",");
//		List<String> groups = Lists.newArrayList();
//		for(String code : groupCodes) {
//			if (StringUtils.isNotEmpty(code)) {
//				Group group = groupDao.findByCode(code);
//				if (group != null) {
//					groups.add(group.getId());
//				}
//			}
//		}
//		user.setUserGroups(groups);
//		
//		String[] dataPermissionIds = userImportData.getDataPermissions().split(",");
//		List<String> dataPermissions = Lists.newArrayList();
//		for(String dataPermission : dataPermissionIds) {
//			if (StringUtils.isNotEmpty(dataPermission)) {
//				if ("所有".equals(dataPermission) || "全国".equals(dataPermission)) {
//					dataPermissions.add(SecurityUtils.getAllShopid());
//				} else {
//					dataPermissions.add(dataPermission);
//				}
//			}
//		}
//		user.setDataPermission(dataPermissions);
//		userDao.save(user);
		
	}

	
	
}
